﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;

    public class DegreeSelector extends Sprite
    {
        var tb:NumberInput;
        var topic:Literal;
        var line:Sprite;
        var circle:Sprite;

        public function DegreeSelector(param1:String, param2:int)
        {
            this.circle = new Sprite();
            this.circle.graphics.beginFill(4412799);
            this.circle.graphics.drawCircle(0, 0, 18);
            this.circle.graphics.beginFill(15921905);
            this.circle.graphics.drawCircle(0, 0, 16);
            this.circle.graphics.beginFill(0);
            this.circle.graphics.drawCircle(0, 0, 2);
            this.circle.graphics.endFill();
            this.circle.addEventListener(MouseEvent.MOUSE_DOWN, this.MouseDown, false, 1, true);
            this.circle.x = 18;
            this.circle.y = 10;
            addChild(this.circle);
            this.line = new Sprite();
            this.line.graphics.lineStyle(2, 0, 1, false);
            this.line.graphics.moveTo(0, 0);
            this.line.graphics.lineTo(17, 0);
            this.line.rotation = param2;
            this.circle.addChild(this.line);
            this.topic = new Literal(param1);
            this.topic.x = -this.topic.width - 2;
            addChild(this.topic);
            this.tb = new NumberInput(40, param2, -180, 180);
            this.tb.addEventListener(NumberEvent.CHANGE, this.TextValueChange, false, 1, true);
            this.tb.x = 40;
            this.tb.y = 0;
            addChild(this.tb);
            var _loc_3:* = new Literal("°");
            _loc_3.x = 80;
            _loc_3.y = 0;
            addChild(_loc_3);
            return;
        }// end function

        public function MouseDown(event:MouseEvent) : void
        {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.CalcDegree, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
            this.CalcDegree();
            return;
        }// end function

        public function MouseUp(event:MouseEvent) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.CalcDegree, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            dispatchEvent(new SliderEvent(SliderEvent.CHANGE, this.value));
            return;
        }// end function

        public function CalcDegree(... args) : void
        {
            args = Math.atan2(this.circle.mouseY, this.circle.mouseX) / (Math.PI / 180);
            this.line.rotation = args;
            this.tb.value = Math.round(args);
            dispatchEvent(new SliderEvent(SliderEvent.DRAG, this.value));
            return;
        }// end function

        public function TextValueChange(event:NumberEvent) : void
        {
            this.line.rotation = this.value;
            dispatchEvent(new SliderEvent(SliderEvent.CHANGE, this.value));
            dispatchEvent(new SliderEvent(SliderEvent.DRAG, this.value));
            return;
        }// end function

        public function set value(core.event:int) : void
        {
            this.line.rotation = core.event;
            this.tb.value = core.event;
            return;
        }// end function

        public function get value() : int
        {
            return this.tb.value;
        }// end function

    }
}
